<template>
  <el-form
    :rules="registerRules"
    :model="registerUser"
    ref="registerForm"
    status-icon
    label-width="100px"
    class="registerForm sign-up-form"
  >
    <el-form-item label="用户名" prop="name">
      <el-input
        placeholder="Enter userName..."
        v-model="registerUser.name"
      ></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input
        placeholder="Enter Email..."
        v-model="registerUser.email"
      ></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input
        v-model="registerUser.password"
        type="password"
        placeholder="Enter password..."
      ></el-input>
    </el-form-item>
    <el-form-item label="确认密码" prop="passwordCon">
      <el-input
        v-model="registerUser.passwordCon"
        type="password"
        placeholder="Confirm your password..."
      ></el-input>
    </el-form-item>
    <el-form-item label="选择身份" prop="role">
      <el-select v-model="registerUser.role" placeholder="请选择身份">
        <el-option label="管理员" value="admin"></el-option>
        <el-option label="用户" value="user"></el-option>
        <el-option label="游客" value="visitor"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button
        @click="handleRegister('registerForm')"
        type="primary"
        class="submit-btn"
        >注册</el-button
      >
    </el-form-item>
  </el-form>
</template>
<script lang="ts">
import { ref, getCurrentInstance } from 'vue';
import {userRouter} from 'vue-router'
export default {
  props: {
    registerUser: {
      type: Object,
      required: true,
    },
    registerRules: {
      type: Object,
      required: true,
    },
  },
  setup() {
    // @ ts-ignore
    const { ctx } = getCurrentInstance();
    const router=userRouter()
    // 触发登陆方法
    const handleRegister = (formName: string) => {
      ctx.$refs[formName].validate((valid: boolean) => {
        if (valid) {
          ctx.$axios.post(
            "URL",
            props.registerUser
          ).then((res:any)=>{
            ctx.$message({
              message:'注册成功',
              type:"success"
            })
            // 路由跳转
            router.push("/")
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    };
    return { handleRegister };
  },
};
</script>
